package service

import (
	"RestaurantApp/dao"
	"RestaurantApp/model"
	"sort"
)

type ShopService struct {
}

func (this *ShopService) GetShopByCategoryId(categoryId int) []model.Shop {

	shopDao := dao.ShopDao{}
	shops := shopDao.FindShopByCategoryId(categoryId)
	return shops

}

func (this *ShopService) GetEveryShop() []model.Shop {
	shopDao := dao.ShopDao{}
	shops := shopDao.FindEveryShop()
	return shops
}

func (this *ShopService) GetShopByPosition(longitude, latitude float64) []model.Shop {
	shopDao := dao.ShopDao{}
	shops := shopDao.FindEveryShop()
	sort.Slice(shops, func(i, j int) bool {
		ax := shops[i].Longitude
		ay := shops[i].Latitude
		bx := shops[j].Longitude
		by := shops[j].Latitude
		disA := (ax-longitude)*(ax-longitude) + (ay-latitude)*(ay-latitude)
		disB := (bx-longitude)*(bx-longitude) + (by-latitude)*(by-latitude)
		return disA < disB
	})
	return shops

}

func (this *ShopService) GetShopByKeyword(keyword string) []model.Shop {
	shopDao := dao.ShopDao{}
	shops := shopDao.FindShopByKeyword(keyword)
	return shops
}
